package cn.stylefeng.guns.sys.core.redis;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.consts.CommonConstant;
import cn.stylefeng.guns.sys.core.cache.RedisCache;
import cn.stylefeng.guns.sys.modular.user.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * 用户锁定key过期监听器类
 *
 * @author zhushengjie
 * @date 2022-09-09
 */
@Slf4j
@Component
public class UserLoginFailedKeyExpirationListener extends AbstractRedisKeyExpirationListener {

    @Autowired
    private SysUserService sysUserService;


    public UserLoginFailedKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    String needHandleExpKeyPrefix() {
        return CommonConstant.USER_LOCK_PREFIX;
    }

    @Override
    void handleMessage(String expKey) {
        String expUserAccount = StrUtil.subAfter(expKey, needHandleExpKeyPrefix(), true);
        log.info(">>> 检测到用户: [{}] 登录失败锁定已过期", expUserAccount);
        //用户锁定过期后解锁用户, 修改用户状态、清空登录失败次数及登录失败时间
        boolean unLockResult = sysUserService.unLockUserClearLoginFailFlag(expUserAccount);
        log.info(">>> 用户: [{}] 解锁完成, 解锁结果: {}", expUserAccount, unLockResult);
    }
}
